package controller

import (
	"fmt"
	"i18n-template/components"
	"i18n-template/table"
	"i18n-template/utils"
	"net/url"

	"github.com/gin-gonic/gin"
)

// compolate 0
func GenerateProductController() gin.HandlerFunc {
	return func(context *gin.Context) {
		t := context.Query("type")
		pro, err := TypeParseProduct(t, context, 0)
		if err != nil {
			utils.FailResponse(context, "获取数据失败", err.Error())
			return
		}
		fmt.Println("绑定数据成功!!!!", pro)
		err = table.GenerateProductService(t, pro)
		fmt.Println("失败了嘛?!!!!", err)
		if err != nil {
			utils.FailResponse(context, "添加商品失败, 请联系管理员", err.Error())
			return
		}
		utils.SuccessResponse(context, "添加商品成功!", pro)
	}
}

// compolate
func FindProductController() gin.HandlerFunc {
	return func(context *gin.Context) {
		path := context.Request.URL.Query()
		product, err := TypeFindProduct(path)
		fmt.Println("=> product ", path)
		if err != nil {
			utils.FailResponse(context, "查询失败！", err.Error())
			return
		}
		utils.SuccessResponse(context, "查询成功!", product)
	}
}

// compolate
func SearchProductController() gin.HandlerFunc {
	return func(context *gin.Context) {
		h := map[string]string{}
		url := context.Request.URL.Query()
		err := context.ShouldBindJSON(&h)
		if err != nil {
			utils.FailResponse(context, "参数错误", err.Error())
			return
		}
		for key, value := range url {
			h[key] = value[0]
		}
		queryURL := ""
		t := ""
		for key, value := range h {
			if queryURL == "" {
				queryURL = fmt.Sprintf("%s = '%s'", key, value)
			} else {
				queryURL += fmt.Sprintf(" AND %s = '%s'", key, value)
			}
			if t == "" && key == "Type" {
				t = value
			}
		}
		switch t {
		case "电感式传感器", "InductiveSensor", "Индуктивныйдатчик":
			pro := make([]table.Inductance, 100)
			err = components.DB.Model(&table.Inductance{}).
				Where(queryURL).Order("id desc").Find(&pro).Error
			if err != nil {
				utils.FailResponse(context, "查询商品失败!", err.Error())
				return
			}
			utils.SuccessResponse(context, "查询成功!", pro)
		case "光电式传感器", "PhotoelectricSensor", "Фотоэлектрическийдатчик":
			pro := make([]table.Photoelectric, 100)
			err = components.DB.Model(&table.Photoelectric{}).
				Where(queryURL).Order("id desc").Find(&pro).Error
			if err != nil {
				utils.FailResponse(context, "查询商品失败!", err.Error())
				return
			}
			utils.SuccessResponse(context, "查询成功!", pro)
		case "电容式传感器", "CapacitiveSensor", "Конденсаторныйдатчик":
			pro := make([]table.Capacitance, 100)
			err = components.DB.
				Model(&table.Capacitance{}).
				Where(queryURL).Order("id desc").
				Find(&pro).
				Error
			if err != nil {
				utils.FailResponse(context, "查询商品失败!", err.Error())
				return
			}
			utils.SuccessResponse(context, "查询成功!", pro)
		case "测量传感器", "MeasurementSensor", "Измерительныйдатчик":
			pro := make([]table.Measurement, 300)
			err = components.DB.Model(&table.Measurement{}).
				Where(queryURL).Order("id desc").Find(&pro).Error
			if err != nil {
				utils.FailResponse(context, "查询商品失败!", err.Error())
				return
			}
			utils.SuccessResponse(context, "查询成功!", pro)
		default:
			utils.SuccessResponse(context, "条件没有达成!", nil)

		}
	}
}

// compolate 2
func DeleteProductController() gin.HandlerFunc {
	return func(context *gin.Context) {
		t := context.Query("type")
		pro, err := TypeParseProduct(t, context, 2)
		if err != nil {
			utils.FailResponse(context, "删除失败, 请联系管理员!", err.Error())
			return
		}
		utils.SuccessResponse(context, "删除成功!", pro)
	}
}

// compolate 1
func UpdateProductController() gin.HandlerFunc {
	return func(context *gin.Context) {
		t := context.Query("type")
		pro, err := TypeParseProduct(t, context, 1)
		if err != nil {
			utils.FailResponse(context, "数据绑定失败", err.Error())
			return
		}
		utils.SuccessResponse(context, "更新商品成功!", pro)
	}
}

// TypeFindProduct 查询商品
func TypeFindProduct(url url.Values) ([]interface{}, error) {
	queryURL := ""
	t := ""
	for key, value := range url {
		if queryURL == "" {
			queryURL = fmt.Sprintf("%s LIKE '%s'", key, "%"+value[0]+"%")
		} else {
			queryURL += fmt.Sprintf(" AND %s LIKE '%s'", key, "%"+value[0]+"%")
		}
		if t == "" && key == "Type" {
			t = value[0]
		}
	}
	fmt.Println("-> ", queryURL, t)
	switch t {
	case "电感式传感器", "InductiveSensor", "Индуктивныйдатчик":
		pro := make([]table.Inductance, 100)
		err := components.DB.Model(&table.Inductance{}).
			Where(queryURL).Order("id desc").Find(&pro).Error
		data := make([]interface{}, len(pro))
		for i, v := range pro {
			data[i] = v
		}
		return data, err
	case "光电式传感器", "PhotoelectricSensor", "Фотоэлектрическийдатчик":
		pro := make([]table.Photoelectric, 100)
		err := components.DB.Model(&table.Photoelectric{}).
			Where(queryURL).Order("id desc").Find(&pro).Error
		data := make([]interface{}, len(pro))
		for i, v := range pro {
			data[i] = v
		}
		return data, err
	case "电容式传感器", "CapacitiveSensor", "Конденсаторныйдатчик":
		pro := make([]table.Capacitance, 100)
		err := components.DB.
			Model(&table.Capacitance{}).
			Where(queryURL).Order("id desc").
			Find(&pro).
			Error
		data := make([]interface{}, len(pro))
		for i, v := range pro {
			data[i] = v
		}
		return data, err
	case "测量传感器", "MeasurementSensor", "Измерительныйдатчик":
		pro := make([]table.Measurement, 300)
		err := components.DB.Model(&table.Measurement{}).
			Where(queryURL).Order("id desc").Find(&pro).Error
		data := make([]interface{}, len(pro))
		for i, v := range pro {
			data[i] = v
		}
		return data, err
	default:
		return nil, nil
	}
}

// TypeParseProduct 绑定数据
func TypeParseProduct(class string, context *gin.Context, flag int) (interface{}, error) {
	switch class {
	case "光电式传感器", "PhotoelectricSensor", "Фотоэлектрическийдатчик":
		pro := &table.Photoelectric{}
		err := context.ShouldBind(&pro)
		if flag == 1 {
			err = components.DB.Model(&pro).
				Where("id = ?", pro.ID).
				Updates(pro).Error
			return pro, err
		} else if flag == 2 {
			err = components.DB.Model(&pro).
				Where("id = ?", pro.ID).
				First(&pro).
				Delete(pro).Error
			return pro, err
		}
		return pro, err
	case "电感式传感器", "InductiveSensor", "Индуктивныйдатчик":
		pro := &table.Inductance{}
		err := context.ShouldBind(&pro)
		if flag == 1 {
			err = components.DB.Model(&pro).
				Where("id = ?", pro.ID).
				Updates(pro).Error
			return pro, err
		} else if flag == 2 {
			err = components.DB.Model(&pro).
				Where("id = ?", pro.ID).
				First(&pro).
				Delete(pro).Error
			return pro, err
		}
		return pro, err
	case "电容式传感器", "CapacitiveSensor", "Конденсаторныйдатчик":
		pro := &table.Capacitance{}
		err := context.ShouldBind(&pro)
		if flag == 1 {
			err = components.DB.Model(&pro).Where("id = ?", pro.ID).Updates(pro).Error
			return pro, err
		} else if flag == 2 {
			err = components.DB.Model(&pro).Where("id = ?", pro.ID).First(&pro).Delete(pro).Error
			return pro, err
		}
		return pro, err
	case "测量传感器", "MeasurementSensor", "Измерительныйдатчик":
		pro := &table.Measurement{}
		fmt.Println("=> ce", class)
		err := context.ShouldBind(&pro)
		if flag == 1 {
			err = components.DB.Model(&pro).Where("id = ?", pro.ID).Updates(pro).Error
			return pro, err
		} else if flag == 2 {
			err = components.DB.Model(&pro).Where("id = ?", pro.ID).First(&pro).Delete(pro).Error
			return pro, err
		}
		fmt.Println("=> ce1", class)
		return pro, err
	default:
		pro := &table.Photoelectric{}
		context.ShouldBind(&pro)
		return pro, nil
	}
}
